/*
 * Created on Jun 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.ui.thumbnail;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Composite;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.UploadImage;
import org.scohen.juploadr.ui.ReusableUIFactory;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class UploadStateThumbnailDecorator extends AbstractThumbnailDecorator {
    private static final int PHOTO_COLOR_DEPTH = 32;
    private Image uploading;
    private Image uploaded;
    private ReusableUIFactory uiFactory = ReusableUIFactory.getInstance();

    public UploadStateThumbnailDecorator(Composite parent,
            ImageAttributes atts, Thumbnail t) {
        super(parent, atts, t);
        uploading = uiFactory.getUploadingImage();
        uploaded = uiFactory.getUploadedImage();

    }

    void paintControl(PaintEvent event) {
        ImageData imageData = getImage().getImageData();
        GC gc = event.gc;

        if (attrs.getState() == UploadImage.STATE_UPLOADED) {
            ImageData copyData = new ImageData(imageData.width,
                    imageData.height, PHOTO_COLOR_DEPTH, new PaletteData(0xFF,
                            0xFF00, 0xFF0000));
            copyData.alpha = 64;
            Color white = new Color(event.widget.getDisplay(), 255, 255, 255);
            Image copy = new Image(event.widget.getDisplay(), copyData);
            GC cGC = new GC(copy);
            cGC.setBackground(white);
            cGC.fillRectangle(0, 0, copyData.width, copyData.height);
            gc.drawImage(copy, BORDER_WIDTH, BORDER_WIDTH);

            copy.dispose();

            cGC.dispose();
            white.dispose();

            int center = THUMBNAIL_WIDTH / 2;
            int myWidth = uploading.getImageData().width;
            gc.drawImage(uploaded, center - myWidth / 2, BORDER_WIDTH);

        } else if (attrs.getState() == UploadImage.STATE_UPLOADING) {
            int center = THUMBNAIL_WIDTH / 2;
            int myWidth = uploading.getImageData().width;
            gc.drawImage(uploading, center - myWidth / 2, BORDER_WIDTH);
        }
    }

    void widgetDisposed(DisposeEvent e) {
    }

}